package com.jh.paymentchannel.helipay.service;

import java.net.URI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.jh.paymentchannel.pojo.Spend;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.jh.paymentchannel.helipay.util.Disguiser;
import com.jh.paymentchannel.helipay.util.HeliPayBase;
import com.jh.paymentchannel.helipay.util.HttpClientService;
import com.jh.paymentchannel.helipay.util.MyBeanUtils;
import com.jh.paymentchannel.helipay.util.RSA;
import com.jh.paymentchannel.helipay.vo.BalanceQueryResponseVo;
import com.jh.paymentchannel.helipay.vo.BalanceQueryVo;
import com.jh.paymentchannel.helipay.vo.BindCardPayResponseVo;
import com.jh.paymentchannel.helipay.vo.BindCardPayVo;
import com.jh.paymentchannel.helipay.vo.BindCardResponseVo;
import com.jh.paymentchannel.helipay.vo.BindCardVo;
import com.jh.paymentchannel.helipay.vo.ConfirmPayResponseVo;
import com.jh.paymentchannel.helipay.vo.ConfirmPayVo;
import com.jh.paymentchannel.helipay.vo.CreditCardPaymentResponseVo;
import com.jh.paymentchannel.helipay.vo.CreditCardPaymentVo;
import com.jh.paymentchannel.helipay.vo.QueryCredictPaymentResponseVo;
import com.jh.paymentchannel.helipay.vo.QueryOrderResponseVo;
import com.jh.paymentchannel.helipay.vo.QueryOrderVo;
import com.jh.paymentchannel.helipay.vo.SendPayCodeVo;
import com.jh.paymentchannel.helipay.vo.SendValidateCodeResponseVo;
import com.jh.paymentchannel.helipay.vo.SendValidateCodeVo;
import com.jh.paymentchannel.helipay.vo.SettlementCardBindResponseVo;
import com.jh.paymentchannel.helipay.vo.SettlementCardBindVo;
import com.jh.paymentchannel.helipay.vo.SettlementCardWithdrawResponseVo;
import com.jh.paymentchannel.helipay.vo.SettlementCardWithdrawVo;
import com.jh.paymentchannel.service.SpendService;
import com.jh.paymentchannel.util.Util;

//快捷支付接口方式
@Controller
@EnableAutoConfiguration
public class QuickPayApi extends HeliPayBase {
	@Autowired
	Util util;
	@Autowired
	private SpendService ss;
    private static final Log log = LogFactory.getLog(QuickPayApi.class);
    @RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/topup/creditPay")
    public @ResponseBody Object confirmPay(
    		@RequestParam(value = "P1_bizType", required = false) String P1_bizType,
    		@RequestParam(value = "P2_customerNumber", required = false) String P2_customerNumber,
    		@RequestParam(value = "P3_userId", required = false) String P3_userId,
    		@RequestParam(value = "P4_bindId", required = false) String P4_bindId,
    		@RequestParam(value = "P5_orderId", required = false) String P5_orderId,
    		@RequestParam(value = "P6_timestamp", required = false) String P6_timestamp,
    		@RequestParam(value = "P7_currency", required = false) String P7_currency,
    		@RequestParam(value = "P8_orderAmount", required = false) String P8_orderAmount,
    		@RequestParam(value = "P9_feeType", required = false) String P9_feeType,
    		@RequestParam(value = "P10_summary", required = false) String P10_summary,
    		@ModelAttribute(value = "CreditCardPaymentVo") CreditCardPaymentVo model) {
        log.info("--------进入信用卡还款接口----------");
        Map mav = new HashMap();
        //mav.setViewName("quickPayApi/response");
        try {
            Map<String, String> map = MyBeanUtils.convertBean(model, new LinkedHashMap());
            String oriMessage = MyBeanUtils.getSigned(map, null);
            oriMessage = oriMessage.substring(0, oriMessage.lastIndexOf(HeliPayBase.SPLIT));
            log.info("签名原文串：" + oriMessage);
            String sign = RSA.sign(oriMessage, RSA.getPrivateKey(HeliPayBase.SIGNKEY_TRANSFER_RSA));
            log.info("签名串：" + sign);
            map.put("sign", sign);
            log.info("发送参数：" + map);
            Map<String, Object> resultMap = HttpClientService.getHttpResp(map, REQUEST_URL_TRANSFER);
            log.info("响应结果：" + resultMap);
            if ((Integer) (resultMap.get("statusCode")) == HttpStatus.SC_OK) {
                String resultMsg = (String) resultMap.get("response");
                if(resultMsg.contains("REQUIRED")) {
                	mav.put("message", "请求签名错误");
                	return mav;
                }
                CreditCardPaymentResponseVo confirmPayResponseVo = JSONObject.parseObject(resultMsg, CreditCardPaymentResponseVo.class);
                String[] excludes = {"rt3_retMsg"};
                String assemblyRespOriSign = MyBeanUtils.getSigned(confirmPayResponseVo, excludes);
                log.info("assemblyRespOriSign：" + assemblyRespOriSign);
                assemblyRespOriSign = assemblyRespOriSign.substring(0, assemblyRespOriSign.lastIndexOf(HeliPayBase.SPLIT) + 1);
                log.info("assemblyRespOriSign：" + assemblyRespOriSign);
                assemblyRespOriSign += SIGNKEY_TRANSFER_MD5;
                log.info("组装返回结果签名串：" + assemblyRespOriSign);
                String responseSign = confirmPayResponseVo.getSign();
                log.info("响应签名：" + responseSign);
                String checkSign = Disguiser.disguiseMD5(assemblyRespOriSign.trim());
                log.info("验证签名串：" + checkSign);
                if (checkSign.equals(responseSign)) {
                    if ("0000".equals(confirmPayResponseVo.getRt2_retCode())) {
                        mav.put("message", "交易成功");
                        mav.put("json", JSONObject.parseObject(resultMsg));
                    } else {
                        mav.put("message", confirmPayResponseVo.getRt3_retMsg());
                    }
                } else {
                    mav.put("message", "验签失败");
                }
            } else {
                mav.put("message", "请求失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            mav.put("message", "交易异常：" + e.getMessage());
        }
        return mav;
    }
    @RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/topup/createOrder")
    public @ResponseBody Object createOrder(
    		@RequestParam(value = "P1_bizType", required = false) String P1_bizType,
    		@RequestParam(value = "P2_customerNumber", required = false) String P2_customerNumber,
    		@RequestParam(value = "P3_userId", required = false) String P3_userId,
    		@RequestParam(value = "P4_timestamp", required = false) String P4_timestamp,
    		@ModelAttribute(value = "BalanceQueryVo") BalanceQueryVo orderVo) {
        log.info("--------进入用户余额查询接口----------");
        Map mav = new HashMap();
        //mav.setViewName("quickPayApi/createOrder");
        try {
            Map<String, String> map = MyBeanUtils.convertBean(orderVo, new LinkedHashMap());
            String oriMessage = MyBeanUtils.getSigned(map, null);
            log.info("签名原文串：" + oriMessage);
            String sign = Disguiser.disguiseMD5(oriMessage.trim());
            log.info("签名串：" + sign);
            map.put("sign", sign);
            log.info("发送参数：" + map);
            Map<String, Object> resultMap = HttpClientService.getHttpResp(map,REQUEST_URL_SAME_PERSON);
            log.info("响应结果：" + resultMap);
            if ((Integer) resultMap.get("statusCode") == HttpStatus.SC_OK) {
                String resultMsg = (String) resultMap.get("response");
                if(resultMsg.contains("REQUIRED")) {
                	mav.put("message", "请求签名错误");
                	return mav;
                }
                BalanceQueryResponseVo orderResponseVo = JSONObject.parseObject(resultMsg, BalanceQueryResponseVo.class);
                //String[] excludes = {"rt3_retMsg"};
                String assemblyRespOriSign = MyBeanUtils.getSigned(orderResponseVo, null);
                log.info("组装返回结果签名串：" + assemblyRespOriSign);
                String responseSign = orderResponseVo.getSign();
                log.info("响应签名：" + responseSign);
                String checkSign = Disguiser.disguiseMD5(assemblyRespOriSign.trim());
                log.info("验证签名：" + checkSign);
                if (checkSign.equals(responseSign)) {
                    if ("0000".equals(orderResponseVo.getRt2_retCode())) {
                    	mav.put("message", "查询成功");
                        mav.put("json", JSONObject.parseObject(resultMsg));
                    } else {
                        mav.put("message", orderResponseVo.getRt3_retMsg());
                    }
                } else {
                    mav.put("message", "验签失败");
                }
            } else {
                mav.put("message", "请求失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            mav.put("message", "交易异常：" + e.getMessage());
        }
        return mav;
    }

    @RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/topup/sendValidateCode")
    public @ResponseBody Object sendValidateCode(
    		@RequestParam(value = "P1_bizType", required = false) String P1_bizType,
    		@RequestParam(value = "P2_customerNumber", required = false) String P2_customerNumber,
    		@RequestParam(value = "P3_userId", required = false) String P3_userId,
    		@RequestParam(value = "P4_orderId", required = false) String P4_orderId,
    		@RequestParam(value = "P5_timestamp", required = false) String P5_timestamp,
    		@RequestParam(value = "P6_cardNo", required = false) String P6_cardNo,
    		@RequestParam(value = "P7_phone", required = false) String P7_phone,
    		@ModelAttribute(value = "SendValidateCodeVo") SendValidateCodeVo sendVo) {
        log.info("--------进入鉴权绑卡短信接口----------");
        //JSONObject json = new JSONObject();
        Map json = new HashMap();
        try {
            Map<String, String> map = MyBeanUtils.convertBean(sendVo, new LinkedHashMap());
            String oriMessage = MyBeanUtils.getSigned(map, null);
            log.info("签名原文串：" + oriMessage);
            String sign = Disguiser.disguiseMD5(oriMessage.trim());
            log.info("签名串：" + sign);
            map.put("sign", sign);
            log.info("发送参数：" + map);
            Map<String, Object> resultMap = HttpClientService.getHttpResp(map, REQUEST_URL_SAME_PERSON);
            log.info("响应结果：" + resultMap);
            if ((Integer) (resultMap.get("statusCode")) == HttpStatus.SC_OK) {
                String resultMsg = (String) resultMap.get("response");
                if(resultMsg.contains("REQUIRED")) {
                	json.put("message", "请求签名错误");
                	return json;
                }
                SendValidateCodeResponseVo sendResponseVo = JSONObject.parseObject(resultMsg, SendValidateCodeResponseVo.class);
                String[] excludes = {"rt3_retMsg"};
                String assemblyRespOriSign = MyBeanUtils.getSigned(sendResponseVo, excludes);
                log.info("组装返回结果签名串：" + assemblyRespOriSign);
                String responseSign = sendResponseVo.getSign();
                log.info("响应签名：" + responseSign);
                String checkSign = Disguiser.disguiseMD5(assemblyRespOriSign.trim());
                log.info("验证签名：" + checkSign);
                if (checkSign.equals(responseSign)) {
                    if ("0000".equals(sendResponseVo.getRt2_retCode())) {
                        json.put("code", "00");
                    } else {
                        json.put("code", "01");
                    }
                    json.put("message", sendResponseVo.getRt3_retMsg());
                } else {
                    json.put("code", "01");
                    json.put("message", "验签失败");
                }
            } else {
                json.put("code", "01");
                json.put("message", "请求失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.put("code", "01");
            json.put("message", "请求异常：" + e.getMessage());
        }
        return json;
    }
    @RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/topup/sendPayCode")
    public @ResponseBody Object sendPayCode(
    		@RequestParam(value = "P1_bizType", required = false) String P1_bizType,
    		@RequestParam(value = "P2_customerNumber", required = false) String P2_customerNumber,
    		@RequestParam(value = "P3_bindId", required = false) String P3_bindId,
    		@RequestParam(value = "P4_userId", required = false) String P4_userId,
    		@RequestParam(value = "P5_orderId", required = false) String P5_orderId,
    		@RequestParam(value = "P6_timestamp", required = false) String P6_timestamp,
    		@RequestParam(value = "P7_currency", required = false) String P7_currency,
    		@RequestParam(value = "P8_orderAmount", required = false) String P8_orderAmount,
    		@RequestParam(value = "P9_phone", required = false) String P9_phone,
    		@ModelAttribute(value = "SendPayCodeVo") SendPayCodeVo sendVo) {
        log.info("--------进入绑卡支付短信接口----------");
        //JSONObject json = new JSONObject();
        Map json = new HashMap();
        try {
            Map<String, String> map = MyBeanUtils.convertBean(sendVo, new LinkedHashMap());
            String oriMessage = MyBeanUtils.getSigned(map, null);
            log.info("签名原文串：" + oriMessage);
            String sign = Disguiser.disguiseMD5(oriMessage.trim());
            log.info("签名串：" + sign);
            map.put("sign", sign);
            log.info("发送参数：" + map);
            Map<String, Object> resultMap = HttpClientService.getHttpResp(map, REQUEST_URL_SAME_PERSON);
            log.info("响应结果：" + resultMap);
            if ((Integer) (resultMap.get("statusCode")) == HttpStatus.SC_OK) {
                String resultMsg = (String) resultMap.get("response");
                if(resultMsg.contains("REQUIRED")) {
                	json.put("message", "请求签名错误");
                	return json;
                }
                SendValidateCodeResponseVo sendResponseVo = JSONObject.parseObject(resultMsg, SendValidateCodeResponseVo.class);
                String[] excludes = {"rt3_retMsg"};
                String assemblyRespOriSign = MyBeanUtils.getSigned(sendResponseVo, excludes);
                log.info("组装返回结果签名串：" + assemblyRespOriSign);
                String responseSign = sendResponseVo.getSign();
                log.info("响应签名：" + responseSign);
                String checkSign = Disguiser.disguiseMD5(assemblyRespOriSign.trim());
                log.info("验证签名：" + checkSign);
                if (checkSign.equals(responseSign)) {
                    if ("0000".equals(sendResponseVo.getRt2_retCode())) {
                        json.put("code", "00");
                    } else {
                        json.put("code", "01");
                    }
                    json.put("message", sendResponseVo.getRt3_retMsg());
                } else {
                    json.put("code", "01");
                    json.put("message", "验签失败");
                }
            } else {
                json.put("code", "01");
                json.put("message", "请求失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.put("code", "01");
            json.put("message", "请求异常：" + e.getMessage());
        }
        return json;
    }

    @RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/topup/confirmPay")
    public @ResponseBody Object confirmPay(@ModelAttribute(value = "ConfirmPayVo") ConfirmPayVo model) {
        log.info("--------进入确认支付接口----------");
        ModelAndView mav = new ModelAndView();
        mav.setViewName("quickPayApi/response");
        try {
            Map<String, String> map = MyBeanUtils.convertBean(model, new LinkedHashMap());
            String oriMessage = MyBeanUtils.getSigned(map, null);
            log.info("签名原文串：" + oriMessage);
            String sign = Disguiser.disguiseMD5(oriMessage.trim());
            log.info("签名串：" + sign);
            map.put("sign", sign);
            log.info("发送参数：" + map);
            Map<String, Object> resultMap = HttpClientService.getHttpResp(map, REQUEST_URL_SAME_PERSON);
            log.info("响应结果：" + resultMap);
            if ((Integer) (resultMap.get("statusCode")) == HttpStatus.SC_OK) {
                String resultMsg = (String) resultMap.get("response");
                ConfirmPayResponseVo confirmPayResponseVo = JSONObject.parseObject(resultMsg, ConfirmPayResponseVo.class);
                String assemblyRespOriSign = MyBeanUtils.getSigned(confirmPayResponseVo, null);
                log.info("组装返回结果签名串：" + assemblyRespOriSign);
                String responseSign = confirmPayResponseVo.getSign();
                log.info("响应签名：" + responseSign);
                String checkSign = Disguiser.disguiseMD5(assemblyRespOriSign.trim());
                if (checkSign.equals(responseSign)) {
                    if ("0000".equals(confirmPayResponseVo.getRt2_retCode())) {
                        mav.addObject("message", "交易成功");
                        mav.addObject("json", JSONObject.parseObject(resultMsg));
                    } else {
                        mav.addObject("message", confirmPayResponseVo.getRt3_retMsg());
                    }
                } else {
                    mav.addObject("message", "验签失败");
                }
            } else {
                mav.addObject("message", "请求失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            mav.addObject("message", "交易异常：" + e.getMessage());
        }
        return mav;
    }
    @RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/topup/bindCardPayfirst")
    public @ResponseBody Object bindCardPayFirst(HttpServletRequest request,
    		@RequestParam(value = "P1_bizType", required = false) String P1_bizType,
    		@RequestParam(value = "P2_customerNumber", required = false) String P2_customerNumber,
    		@RequestParam(value = "P3_bindId", required = false) String P3_bindId,
    		@RequestParam(value = "P4_userId", required = false) String P4_userId,
    		@RequestParam(value = "P5_orderId", required = false) String P5_orderId,
    		@RequestParam(value = "P6_timestamp", required = false) String P6_timestamp,
    		@RequestParam(value = "P7_currency", required = false) String P7_currency,
    		@RequestParam(value = "P8_orderAmount", required = false) String P8_orderAmount,
    		@RequestParam(value = "P9_goodsName", required = false) String P9_goodsName,
    		@RequestParam(value = "P10_goodsDesc", required = false) String P10_goodsDesc,
    		@RequestParam(value = "P11_terminalType", required = false) String P11_terminalType,
    		@RequestParam(value = "P12_terminalId", required = false) String P12_terminalId,
    		@RequestParam(value = "P13_orderIp", required = false) String P13_orderIp,
    		@RequestParam(value = "P14_period", required = false) String P14_period,
    		@RequestParam(value = "P15_periodUnit", required = false) String P15_periodUnit,
    		@RequestParam(value = "P16_serverCallbackUrl", required = false) String P16_serverCallbackUrl,
    		@RequestParam(value = "P17_validateCode", required = false) String P17_validateCode,
    		@ModelAttribute(value = "BindCardPayVo") BindCardPayVo model) throws ParseException {
        log.info("--------进入首次绑卡支付接口----------");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date();
        String time = sdf.format(date);
        Map mav = new HashMap();
        String obj = ss.findByBindId(P3_bindId);
        log.info("==================="+obj+"===================");
        /*Calendar c = Calendar.getInstance();
        c.setTimeInMillis(sdf.parse(obj).getTime());
        if((date.getTime()-c.getTimeInMillis())<300000) {
        	int limitime = (int)(date.getTime()-c.getTimeInMillis())/60000;
        	mav.put("message", "请在"+limitime+"分钟后再次操作！");
        	return mav;
        }*/
        List<Spend> list = null;
        //mav.setViewName("quickPayApi/response");
        if(null==obj||obj.isEmpty()) {
        	RestTemplate restTemplate = new RestTemplate();
        	URI uri = util.getServiceUrl("transactionclear", "error url request!");
        	String url = uri.toString() + "/v1.0/transactionclear/payment/add2";
        	/** 根据的用户手机号码查询用户的基本信息 */
        	MultiValueMap<String,String> requestEntity = new LinkedMultiValueMap<String, String>();
        	requestEntity.add("type", "0");
        	requestEntity.add("userId",P4_userId );
        	requestEntity.add("bankCard",P3_bindId );
        	requestEntity.add("amount", P8_orderAmount);
        	requestEntity.add("orderCode", P5_orderId);
        	requestEntity.add("channel_tag", "HL_PAY");
        	requestEntity.add("desc", "绑卡首次支付验证");
        	Object result = restTemplate.postForObject(url, requestEntity, String.class);
        	log.info("接口/v1.0/transactionclear/payment/add--RESULT================" + result);
        }
        /*Object result = ss.addSpend(request, "5", "2.00", "", "", P4_userId, P3_bindId, "绑卡首次支付验证", P5_orderId, time+":00", "123456"); 
		log.info("===================="+result.toString()+"=====================");*/
        Object result = null;
        try {
            Map<String, String> map = MyBeanUtils.convertBean(model, new LinkedHashMap());
            String[] requestExcludes = {"P17_validateCode"};
            String oriMessage = MyBeanUtils.getSigned(map, requestExcludes);
            log.info("签名原文串：" + oriMessage);
            String sign = Disguiser.disguiseMD5(oriMessage.trim());
            log.info("签名串：" + sign);
            map.put("sign", sign);
            log.info("发送参数：" + map);
            Map<String, Object> resultMap = HttpClientService.getHttpResp(map, REQUEST_URL_SAME_PERSON);
            log.info("响应结果：" + resultMap);
            if ((Integer) resultMap.get("statusCode") == HttpStatus.SC_OK) {
                String resultMsg = (String) resultMap.get("response");
                if(resultMsg.contains("REQUIRED")) {
                	mav.put("message", "请求签名错误");
                	return mav;
                }
                BindCardPayResponseVo bindCardPayResponseVo = JSONObject.parseObject(resultMsg, BindCardPayResponseVo.class);
                //String[] excludes = {"rt3_retMsg"};
                String assemblyRespOriSign = MyBeanUtils.getSigned(bindCardPayResponseVo, null);
                log.info("组装返回结果签名串：" + assemblyRespOriSign);
                String responseSign = bindCardPayResponseVo.getSign();
                log.info("响应签名：" + responseSign);
                String checkSign = Disguiser.disguiseMD5(assemblyRespOriSign.trim());
                log.info("验证签名：" + checkSign);
                if (checkSign.equals(responseSign)) {
                    if ("0000".equals(bindCardPayResponseVo.getRt2_retCode())) {
                        mav.put("message", "绑卡支付交易成功");
                        mav.put("json", JSONObject.parseObject(resultMsg));
                        result = ss.addSpend(request, "3", "2.00", "", "", P4_userId, P3_bindId, "绑卡首次支付验证：成功", P5_orderId, time+":00", "123456"); 
                		log.info("===================="+result.toString()+"=====================");
                    } else {
                        mav.put("message", bindCardPayResponseVo.getRt3_retMsg());
                        result = ss.addSpend(request, "5", "2.00", "", "", P4_userId, P3_bindId, "绑卡首次支付验证："+bindCardPayResponseVo.getRt3_retMsg(), P5_orderId, time+":00", "123456"); 
                		log.info("===================="+result.toString()+"=====================");
                    }
                } else {
                    mav.put("message", "验签失败");
                    result = ss.addSpend(request, "5", "2.00", "", "", P4_userId, P3_bindId, "绑卡首次支付验证：验签失败", P5_orderId, time+":00", "123456"); 
            		log.info("===================="+result.toString()+"=====================");
                }
            } else {
            	if ((Integer) resultMap.get("statusCode") == HttpStatus.SC_BAD_GATEWAY) {
            		mav.put("message", "502");
            		result = ss.addSpend(request, "5", "2.00", "", "", P4_userId, P3_bindId, "绑卡首次支付验证：502", P5_orderId, time+":00", "123456"); 
            		log.info("===================="+result.toString()+"=====================");
            	}else {
            		mav.put("message", "请求失败");
            		result = ss.addSpend(request, "5", "2.00", "", "", P4_userId, P3_bindId, "绑卡首次支付验证：请求失败", P5_orderId, time+":00", "123456"); 
            		log.info("===================="+result.toString()+"=====================");
            	}
            }
        } catch (Exception e) {
            e.printStackTrace();
            mav.put("message", "交易异常：" + e.getMessage());
            result = ss.addSpend(request, "5", "2.00", "", "", P4_userId, P3_bindId, "交易异常：" + e.getMessage(), P5_orderId, time+":00", "123456"); 
    		log.info("===================="+result.toString()+"=====================");
        }
        return mav;
    }
    @RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/topup/bindCardPay")
    public @ResponseBody Object bindCardPay(HttpServletRequest request,
    		@RequestParam(value = "P1_bizType", required = false) String P1_bizType,
    		@RequestParam(value = "P2_customerNumber", required = false) String P2_customerNumber,
    		@RequestParam(value = "P3_bindId", required = false) String P3_bindId,
    		@RequestParam(value = "P4_userId", required = false) String P4_userId,
    		@RequestParam(value = "P5_orderId", required = false) String P5_orderId,
    		@RequestParam(value = "P6_timestamp", required = false) String P6_timestamp,
    		@RequestParam(value = "P7_currency", required = false) String P7_currency,
    		@RequestParam(value = "P8_orderAmount", required = false) String P8_orderAmount,
    		@RequestParam(value = "P9_goodsName", required = false) String P9_goodsName,
    		@RequestParam(value = "P10_goodsDesc", required = false) String P10_goodsDesc,
    		@RequestParam(value = "P11_terminalType", required = false) String P11_terminalType,
    		@RequestParam(value = "P12_terminalId", required = false) String P12_terminalId,
    		@RequestParam(value = "P13_orderIp", required = false) String P13_orderIp,
    		@RequestParam(value = "P14_period", required = false) String P14_period,
    		@RequestParam(value = "P15_periodUnit", required = false) String P15_periodUnit,
    		@RequestParam(value = "P16_serverCallbackUrl", required = false) String P16_serverCallbackUrl,
    		@RequestParam(value = "P17_validateCode", required = false) String P17_validateCode,
    		@ModelAttribute(value = "BindCardPayVo") BindCardPayVo model) {
        log.info("--------进入绑卡支付接口----------");
        Map mav = new HashMap();
        //mav.setViewName("quickPayApi/response");
        try {
            Map<String, String> map = MyBeanUtils.convertBean(model, new LinkedHashMap());
            String[] requestExcludes = {"P17_validateCode"};
            String oriMessage = MyBeanUtils.getSigned(map, requestExcludes);
            log.info("签名原文串：" + oriMessage);
            String sign = Disguiser.disguiseMD5(oriMessage.trim());
            log.info("签名串：" + sign);
            map.put("sign", sign);
            log.info("发送参数：" + map);
            Map<String, Object> resultMap = HttpClientService.getHttpResp(map, REQUEST_URL_SAME_PERSON);
            log.info("响应结果：" + resultMap);
            if ((Integer) resultMap.get("statusCode") == HttpStatus.SC_OK) {
                String resultMsg = (String) resultMap.get("response");
                if(resultMsg.contains("REQUIRED")) {
                	mav.put("message", "请求签名错误");
                	return mav;
                }
                BindCardPayResponseVo bindCardPayResponseVo = JSONObject.parseObject(resultMsg, BindCardPayResponseVo.class);
                //String[] excludes = {"rt3_retMsg"};
                String assemblyRespOriSign = MyBeanUtils.getSigned(bindCardPayResponseVo, null);
                log.info("组装返回结果签名串：" + assemblyRespOriSign);
                String responseSign = bindCardPayResponseVo.getSign();
                log.info("响应签名：" + responseSign);
                String checkSign = Disguiser.disguiseMD5(assemblyRespOriSign.trim());
                log.info("验证签名：" + checkSign);
                if (checkSign.equals(responseSign)) {
                    if ("0000".equals(bindCardPayResponseVo.getRt2_retCode())) {
                        mav.put("message", "绑卡支付交易成功");
                        mav.put("json", JSONObject.parseObject(resultMsg));
                    } else {
                        mav.put("message", bindCardPayResponseVo.getRt3_retMsg());
                    }
                } else {
                    mav.put("message", "验签失败");
                }
            } else {
            	if ((Integer) resultMap.get("statusCode") == HttpStatus.SC_BAD_GATEWAY) {
            		mav.put("message", "502");
            	}else {
            		mav.put("message", "请求失败");
            	}
            }
        } catch (Exception e) {
            e.printStackTrace();
            mav.put("message", "交易异常：" + e.getMessage());
        }
        return mav;
    }

    @RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/topup/queryOrder")
    public @ResponseBody Object queryOrder(
    		@RequestParam(value = "P1_bizType", required = false) String P1_bizType,
    		@RequestParam(value = "P2_orderId", required = false) String P2_orderId,
    		@RequestParam(value = "P3_customerNumber", required = false) String P3_customerNumber,
    		@ModelAttribute(value = "QueryOrderVo") QueryOrderVo model) {
        log.info("--------进入信用卡还款查询接口----------");
        Map mav = new HashMap();
        //mav.setViewName("quickPayApi/response");
        try {
            Map<String, String> map = MyBeanUtils.convertBean(model, new LinkedHashMap());
            String oriMessage = MyBeanUtils.getSigned(map, null);
            String sign = null;
            if("QuickPayQuery".equals(P1_bizType)) {
            	sign = Disguiser.disguiseMD5(oriMessage.trim());
            	log.info("签名原文串：" + oriMessage);
            }else {
            	oriMessage = oriMessage.substring(0, oriMessage.lastIndexOf(HeliPayBase.SPLIT));
            	log.info("签名原文串：" + oriMessage);
            	sign = RSA.sign(oriMessage, RSA.getPrivateKey(HeliPayBase.SIGNKEY_TRANSFER_RSA));
            }
            log.info("签名串：" + sign);
            map.put("sign", sign);
            log.info("发送参数：" + map);
           System.out.println("发送参数：" + map);
           Map<String, Object> resultMap = null;
           if("QuickPayQuery".equals(P1_bizType)) {
           	resultMap = HttpClientService.getHttpResp(map, REQUEST_URL_SAME_PERSON);
           }else {
           	resultMap = HttpClientService.getHttpResp(map, REQUEST_URL_TRANSFER);
           }
            log.info("响应结果：" + resultMap);
            if ((Integer) resultMap.get("statusCode") == HttpStatus.SC_OK) {
                String resultMsg = (String) resultMap.get("response");
                if(resultMsg.contains("REQUIRED")) {
                	mav.put("message", "请求签名错误");
                	return mav;
                }
                String responseSign = null;
                String checkSign = null;
                if("QuickPayQuery".equals(P1_bizType)) {
                	QueryOrderResponseVo queryOrderResponseVo = JSONObject.parseObject(resultMsg, QueryOrderResponseVo.class);
                	String assemblyRespOriSign = MyBeanUtils.getSigned(queryOrderResponseVo, null);
                	//assemblyRespOriSign = assemblyRespOriSign.substring(0, assemblyRespOriSign.lastIndexOf(HeliPayBase.SPLIT) + 1);
                	//assemblyRespOriSign += SIGNKEY_TRANSFER_MD5;
                	log.info("组装返回结果签名串：" + assemblyRespOriSign);
                	 responseSign = queryOrderResponseVo.getSign();
                	log.info("响应签名：" + responseSign);
                	 checkSign = Disguiser.disguiseMD5(assemblyRespOriSign.trim());
                	log.info("验证签名串：" + checkSign);
	             	   if (checkSign.equals(responseSign)) {
	            		   if ("0000".equals(queryOrderResponseVo.getRt2_retCode())) {
	            			   mav.put("message", "订单查询成功");
	            			   mav.put("json", JSONObject.parseObject(resultMsg));
	            		   } else {
	            			   mav.put("message", queryOrderResponseVo.getRt3_retMsg());
	            		   }
	            	   } else {
	            		   mav.put("message", "验签失败");
	            	   }
                   }else {
                	   QueryCredictPaymentResponseVo queryOrderResponseVo = JSONObject.parseObject(resultMsg, QueryCredictPaymentResponseVo.class);
                	   String[] excludes = {"rt3_retMsg"};
                	   String assemblyRespOriSign = MyBeanUtils.getSigned(queryOrderResponseVo, excludes);
                	   assemblyRespOriSign = assemblyRespOriSign.substring(0, assemblyRespOriSign.lastIndexOf(HeliPayBase.SPLIT) + 1);
                	   assemblyRespOriSign += SIGNKEY_TRANSFER_MD5;
                	   log.info("组装返回结果签名串：" + assemblyRespOriSign);
                	    responseSign = queryOrderResponseVo.getSign();
                	   log.info("响应签名：" + responseSign);
                	    checkSign = Disguiser.disguiseMD5(assemblyRespOriSign.trim());
                	   log.info("验证签名串：" + checkSign);
                	   if (checkSign.equals(responseSign)) {
                		   if ("0000".equals(queryOrderResponseVo.getRt2_retCode())) {
                			   mav.put("message", "订单查询成功");
                			   mav.put("json", JSONObject.parseObject(resultMsg));
                		   } else {
                			   mav.put("message", queryOrderResponseVo.getRt3_retMsg());
                		   }
                	   } else {
                		   mav.put("message", "验签失败");
                	   }
                   }
            } else {
                mav.put("message", "请求失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            mav.put("message", "交易异常：" + e.getMessage());
        }
        return mav;
    }
    @RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/topup/settlementCardBind")
    public @ResponseBody Object settlementCardBind(
    		@RequestParam(value = "P1_bizType", required = false) String P1_bizType,
    		@RequestParam(value = "P2_customerNumber", required = false) String P2_customerNumber,
    		@RequestParam(value = "P3_userId", required = false) String P3_userId,
    		@RequestParam(value = "P4_orderId", required = false) String P4_orderId,
    		@RequestParam(value = "P5_payerName", required = false) String P5_payerName,
    		@RequestParam(value = "P6_idCardType", required = false) String P6_idCardType,
    		@RequestParam(value = "P7_idCardNo", required = false) String P7_idCardNo,
    		@RequestParam(value = "P8_cardNo", required = false) String P8_cardNo,
    		@RequestParam(value = "P9_phone", required = false) String P9_phone,
    		@RequestParam(value = "P10_bankUnionCode", required = false) String P10_bankUnionCode,
    		@ModelAttribute(value = "SettlementCardBindVo") SettlementCardBindVo model) {
        log.info("--------进入绑结算卡接口----------");
        Map mav = new HashMap();
        //mav.setViewName("quickPayApi/response");
        try {
            Map<String, String> map = MyBeanUtils.convertBean(model, new LinkedHashMap());
            String oriMessage = MyBeanUtils.getSigned(map, null);
            log.info("签名原文串：" + oriMessage);
            String sign = Disguiser.disguiseMD5(oriMessage.trim());
            log.info("签名串：" + sign);
            map.put("sign", sign);
            log.info("发送参数：" + map);
            Map<String, Object> resultMap = HttpClientService.getHttpResp(map, REQUEST_URL_SAME_PERSON);
            log.info("响应结果：" + resultMap);
            if ((Integer) resultMap.get("statusCode") == HttpStatus.SC_OK) {
                String resultMsg = (String) resultMap.get("response");
                if(resultMsg.contains("REQUIRED")) {
                	mav.put("message", "请求签名错误");
                	return mav;
                }
                SettlementCardBindResponseVo responseVo = JSONObject.parseObject(resultMsg, SettlementCardBindResponseVo.class);
                String[] excludes = {"rt3_retMsg"};
                String assemblyRespOriSign = MyBeanUtils.getSigned(responseVo, excludes);
                log.info("组装返回结果签名串：" + assemblyRespOriSign);
                String responseSign = responseVo.getSign();
                log.info("响应签名：" + responseSign);
                String checkSign = Disguiser.disguiseMD5(assemblyRespOriSign.trim());
                if (checkSign.equals(responseSign)) {
                    if ("0000".equals(responseVo.getRt2_retCode())) {
                        mav.put("message", "绑结算卡成功");
                        mav.put("json", JSONObject.parseObject(resultMsg));
                    } else {
                        mav.put("message", responseVo.getRt3_retMsg());
                    }
                } else {
                    mav.put("message", "验签失败");
                }
            } else {
                mav.put("message", "请求失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            mav.put("message", "交易异常：" + e.getMessage());
        }
        return mav;
    }

    @RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/topup/settlementCardWithdraw")
    public @ResponseBody Object settlementCardWithdraw(
    		@RequestParam(value = "P1_bizType", required = false) String P1_bizType,
    		@RequestParam(value = "P2_customerNumber", required = false) String P2_customerNumber,
    		@RequestParam(value = "P3_userId", required = false) String P3_userId,
    		@RequestParam(value = "P4_orderId", required = false) String P4_orderId,
    		@RequestParam(value = "P5_amount", required = false) String P5_amount,
    		@RequestParam(value = "P6_feeType", required = false) String P6_feeType,
    		@RequestParam(value = "P7_summary", required = false) String P7_summary,
    		@RequestParam(value = "P8_bindId", required = false) String P8_bindId,
    		@ModelAttribute(value = "SettlementCardWithdrawVo") SettlementCardWithdrawVo model) {
        log.info("--------进入结算卡提现接口----------");
        Map mav = new HashMap();
        //mav.setViewName("quickPayApi/response");
        try {
            Map<String, String> map = MyBeanUtils.convertBean(model, new LinkedHashMap());
            String[] excludes2 = {"P8_bindId"};
            String oriMessage = MyBeanUtils.getSigned(model, excludes2);
            oriMessage = oriMessage.substring(0, oriMessage.lastIndexOf(HeliPayBase.SPLIT));
            log.info("签名原文串：" + oriMessage);
            String sign = RSA.sign(oriMessage, RSA.getPrivateKey(HeliPayBase.SIGNKEY_TRANSFER_RSA));
            log.info("签名串：" + sign);
            map.put("sign", sign);
            log.info("发送参数：" + map);
            Map<String, Object> resultMap = HttpClientService.getHttpResp(map, REQUEST_URL_TRANSFER);
            log.info("响应结果：" + resultMap);
            if ((Integer) resultMap.get("statusCode") == HttpStatus.SC_OK) {
                String resultMsg = (String) resultMap.get("response");
                if(resultMsg.contains("REQUIRED")) {
                	mav.put("message", "请求签名错误");
                	return mav;
                }
                SettlementCardWithdrawResponseVo responseVo = JSONObject.parseObject(resultMsg, SettlementCardWithdrawResponseVo.class);
                String[] excludes = {"rt3_retMsg"};
                String assemblyRespOriSign = MyBeanUtils.getSigned(responseVo, excludes);
                assemblyRespOriSign = assemblyRespOriSign.substring(0, assemblyRespOriSign.lastIndexOf(HeliPayBase.SPLIT) + 1);
                assemblyRespOriSign += SIGNKEY_TRANSFER_MD5;
                log.info("组装返回结果签名串：" + assemblyRespOriSign);
                String responseSign = responseVo.getSign();
                log.info("响应签名：" + responseSign);
                String checkSign = Disguiser.disguiseMD5(assemblyRespOriSign.trim());
                log.info("验证签名串：" + checkSign);
                if (checkSign.equals(responseSign)) {
                    if ("0000".equals(responseVo.getRt2_retCode())) {
                        mav.put("message", "结算卡提现成功");
                        mav.put("json", JSONObject.parseObject(resultMsg));
                    } else {
                        mav.put("message", responseVo.getRt3_retMsg());
                    }
                } else {
                    mav.put("message", "验签失败");
                }
            } else {
                mav.put("message", "请求失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            mav.put("message", "交易异常：" + e.getMessage());
        }
        return mav;
    }

    @RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/topup/bindCard")
    public @ResponseBody Object bindCard(HttpServletRequest request,
    		@RequestParam(value = "P1_bizType", required = false) String P1_bizType,
    		@RequestParam(value = "P2_customerNumber", required = false) String P2_customerNumber,
    		@RequestParam(value = "P3_userId", required = false) String P3_userId,
    		@RequestParam(value = "P4_orderId", required = false) String P4_orderId,
    		@RequestParam(value = "P5_timestamp", required = false) String P5_timestamp,
    		@RequestParam(value = "P6_payerName", required = false) String P6_payerName,
    		@RequestParam(value = "P7_idCardType", required = false) String P7_idCardType,
    		@RequestParam(value = "P8_idCardNo", required = false) String P8_idCardNo,
    		@RequestParam(value = "P9_cardNo", required = false) String P9_cardNo,
    		@RequestParam(value = "P10_year", required = false) String P10_year,
    		@RequestParam(value = "P11_month", required = false) String P11_month,
    		@RequestParam(value = "P12_cvv2", required = false) String P12_cvv2,
    		@RequestParam(value = "P13_phone", required = false) String P13_phone,
    		@RequestParam(value = "P14_validateCode", required = false) String P14_validateCode,
    		@ModelAttribute(value = "BindCardVo") BindCardVo model) {
        log.info("--------进入绑卡接口----------");
        Map mav = new HashMap();
        try {
            Map<String, String> map = MyBeanUtils.convertBean(model, new LinkedHashMap());
            log.info("===========map=========" + map.toString());
            String oriMessage = MyBeanUtils.getSigned(map, null);
            log.info("签名原文串：" + oriMessage);
            String sign = Disguiser.disguiseMD5(oriMessage.trim());
            log.info("签名串：" + sign);
            map.put("sign", sign);
            log.info("发送参数：" + map);
            Map<String, Object> resultMap = HttpClientService.getHttpResp(map, REQUEST_URL_SAME_PERSON);
            log.info("响应结果：" + resultMap);
            if ((Integer) resultMap.get("statusCode") == HttpStatus.SC_OK) {
                String resultMsg = (String) resultMap.get("response");
                if(resultMsg.contains("REQUIRED")) {
                	mav.put("message", "请求签名错误");
                	return mav;
                }
                BindCardResponseVo bindCardResponseVo = JSONObject.parseObject(resultMsg, BindCardResponseVo.class);
                String[] excludes = {"rt3_retMsg"};
                String assemblyRespOriSign = MyBeanUtils.getSigned(bindCardResponseVo, excludes);
                log.info("组装返回结果签名串：" + assemblyRespOriSign);
                String responseSign = bindCardResponseVo.getSign();
                log.info("响应签名：" + responseSign);
                String checkSign = Disguiser.disguiseMD5(assemblyRespOriSign.trim());
                log.info("验证签名：" + checkSign);
                if (checkSign.equals(responseSign)) {
                    if ("0000".equals(bindCardResponseVo.getRt2_retCode())) {
                        mav.put("message", "绑卡交易成功");
                        mav.put("json", JSONObject.parseObject(resultMsg));
                    } else {
                        mav.put("message", bindCardResponseVo.getRt3_retMsg());
                    }
                } else {
                    mav.put("message", "验签失败");
                }
            } else {
                mav.put("message", "请求失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            mav.put("message", "交易异常：" + e.getMessage());
        }
        return mav;
    }
    
    
    @RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/topup/bindCardUnbind")
    public @ResponseBody Object bindCardUnbind(HttpServletRequest request,
    		@RequestParam(value = "P1_bizType", required = false) String P1_bizType,
    		@RequestParam(value = "P2_customerNumber", required = false) String P2_customerNumber,
    		@RequestParam(value = "P3_userId", required = false) String P3_userId,
    		@RequestParam(value = "P4_bindId", required = false) String P4_bindId,
    		@RequestParam(value = "P5_orderId", required = false) String P5_orderId,
    		@RequestParam(value = "P6_timestamp", required = false) String P6_timestamp,
    		@ModelAttribute(value = "BindCardPayVo") BindCardPayVo model) {
        log.info("--------进入银行卡解绑接口----------");
        Map mav = new HashMap();
        //mav.setViewName("quickPayApi/response");
        try {
            Map<String, String> map = MyBeanUtils.convertBean(model, new LinkedHashMap());
            String[] requestExcludes = {"P17_validateCode"};
            String oriMessage = MyBeanUtils.getSigned(map, requestExcludes);
            log.info("签名原文串：" + oriMessage);
            String sign = Disguiser.disguiseMD5(oriMessage.trim());
            log.info("签名串：" + sign);
            map.put("sign", sign);
            log.info("发送参数：" + map);
            Map<String, Object> resultMap = HttpClientService.getHttpResp(map, REQUEST_URL_SAME_PERSON);
            log.info("响应结果：" + resultMap);
            if ((Integer) resultMap.get("statusCode") == HttpStatus.SC_OK) {
                String resultMsg = (String) resultMap.get("response");
                if(resultMsg.contains("REQUIRED")) {
                	mav.put("message", "请求签名错误");
                	return mav;
                }
                BindCardPayResponseVo bindCardPayResponseVo = JSONObject.parseObject(resultMsg, BindCardPayResponseVo.class);
                //String[] excludes = {"rt3_retMsg"};
                String assemblyRespOriSign = MyBeanUtils.getSigned(bindCardPayResponseVo, null);
                log.info("组装返回结果签名串：" + assemblyRespOriSign);
                String responseSign = bindCardPayResponseVo.getSign();
                log.info("响应签名：" + responseSign);
                String checkSign = Disguiser.disguiseMD5(assemblyRespOriSign.trim());
                log.info("验证签名：" + checkSign);
                if (checkSign.equals(responseSign)) {
                    if ("0000".equals(bindCardPayResponseVo.getRt2_retCode())) {
                        mav.put("message", "结算卡解绑成功");
                        mav.put("json", JSONObject.parseObject(resultMsg));
                    } else {
                        mav.put("message", bindCardPayResponseVo.getRt3_retMsg());
                    }
                } else {
                    mav.put("message", "验签失败");
                }
            } else {
            	if ((Integer) resultMap.get("statusCode") == HttpStatus.SC_BAD_GATEWAY) {
            		mav.put("message", "502");
            	}else {
            		mav.put("message", "请求失败");
            	}
            }
        } catch (Exception e) {
            e.printStackTrace();
            mav.put("message", "交易异常：" + e.getMessage());
        }
        return mav;
    }
}
